<template>
  <div>
    <el-dialog
      :title="ifAdd + '团队'"
      :visible.sync="addDiavisible"
      width="40%"
      :before-close="cancel"
    >
      <el-form
        :model="form"
        ref="formRef"
        :rules="formRules"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="团队名称" prop="name">
          <el-input :disabled="ifAdd!='新增'" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="选择医生" prop="userIds">
          <el-select v-model="form.userIds" multiple style="width: 100%">
            <el-option 
              v-for="item in options"
			  v-if="item.status==0"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { editGroup, getUser } from "@/api/family_doctor/family_doctor";
import {
  inputValidator,
  selectValidator1,
} from "@/utils/validate";
export default {
  data() {
    return {
      addDiavisible: false,
      data: "",
      placeData: false,
      loading: false,
      ifAdd: "新增",
      form: {
        id: "",
        name: "",
        status:'',
        userIds: [],
      },
      placeId: [],
      optionProps: {
        value: "id",
        label: "name",
        children: "childrenNodes",
      },
      options: [],
      formRules: {
        name: inputValidator,
        userIds: selectValidator1,
      },
    };
  },
  created() {},
  methods: {
    //关闭
    cancel() {
      for (var key in this.form) {
        this.form[key] = "";
      }
      this.form.userIds = []
      this.$refs.formRef.clearValidate();
      this.$emit("transData");
      this.addDiavisible = false;
    },
    //提交
    addSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return this.$message.error("请输入正确的格式!");
        editGroup(this.form).then((res) => {
          if (res.code == "AA000000") {
            this.$message.success(res.msg);
          }
          this.cancel();
        });
      });
    },
    //供父组件调用函数
    openDialog(ifAdd,options,scope) {
      this.options = options
      if (ifAdd == true) {
        this.ifAdd = "新增";
      } else {
        this.ifAdd = "修改";
        this.form.id = scope.id;
        this.form.name = scope.name;
        this.options.forEach(e=>{
          scope.userNameList.forEach(m=>{
			  if(e.status==0){
				  if(m==e.name){
				    this.form.userIds.push(e.id)
				  }
			  }
          })
        })
      }
      this.addDiavisible = true;
    },
  },
};
</script>